﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NetPE.Core;
using NetPE.TreeNodes;

namespace NetPE
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void openToolStripButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "PE File|*.exe;*.dll;*.obj";
            if (ofd.ShowDialog() != DialogResult.OK) return;
            PeFile f;
            if (ofd.FileName.EndsWith(".obj"))
                f = PeFileFactory.Read(ofd.FileName, PeFileType.Object);
            else
                f = PeFileFactory.Read(ofd.FileName, PeFileType.Image);
            tns.Nodes.Add(new FileNode(f));
        }

        private void tns_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if ((e.Node as BaseNode) != null)
            {
                editor.Controls.Clear();
                BaseEditor ed = (e.Node as BaseNode).GetEditorControl();
                ed.Dock = DockStyle.Fill;
                editor.Controls.Add(ed);
                ed.Object = (e.Node as BaseNode).Data;
            }
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "PE File|*.exe;*.dll;*.obj";
            if (tns.SelectedNode == null || !(tns.SelectedNode is FileNode) || sfd.ShowDialog() != DialogResult.OK) return;
            PeFileFactory.Save(sfd.FileName, (tns.SelectedNode as FileNode).Data as PeFile);
        }
    }
}
